package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hxkcloud.nurse.R;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Administrator on 2017/3/28.
 */

public class GroupSelectorPopwindowAdapter extends BaseAdapter {
    private Context context;
    private String[] data;
    private int mSelect = -1;
    private SharedPreferences sps ;
    private SharedPreferences.Editor editors ;
    public GroupSelectorPopwindowAdapter(Context context, String[] data) {
        this.context = context;
        this.data = data;
        sps = context.getSharedPreferences("num",MODE_PRIVATE);
        editors = sps.edit();
        if (sps.getInt("number",-1)!=-1&&sps.getBoolean("numbers",false)!=false) {
            mSelect = sps.getInt("number", -1);
            notifyDataSetChanged();
        }
    }
    public void changeSelected(int posion){
        if (posion!=mSelect){
            editors.putInt("number",posion);
            editors.putBoolean("numbers",true);
            editors.commit();
            mSelect = posion;
            notifyDataSetChanged();
        }
    }
    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolders holders = null;
        if (convertView==null){
            convertView = View.inflate(context, R.layout.activity_groupelectoropwindow_item,null);
            holders = new ViewHolders();
            holders.textView = (TextView) convertView.findViewById(R.id.groupelector_tv);
            convertView.setTag(holders);
        }else {
            holders = (ViewHolders) convertView.getTag();
        }
        holders.textView.setText(data[position].toString());


//                if (sp.getInt("number",-1)==position){
//                    holders.textView.setTextColor(context.getResources().getColor(R.color.blue_text));
//                }

        if (mSelect == position){
            holders.textView.setTextColor(context.getResources().getColor(R.color.blue_text));
        }else {
            holders.textView.setTextColor(context.getResources().getColor(R.color.black_text));
        }

        return convertView;
    }

    static class  ViewHolders{
        TextView textView;
    }
}
